package com.marvels.system.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.marvels.common.controller.BaseController;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.system.permission.dto.McsmUserLoginLog;
import com.marvels.system.permission.dto.SysOrgDTO;
import com.marvels.system.permission.dto.SysPositionDTO;
import com.marvels.system.permission.dto.SysRoleDTO;
import com.marvels.system.permission.dto.SysUserDTO;
import com.marvels.system.permission.service.McsmUserLoginLogService;
import com.marvels.system.permission.service.OrgService;
import com.marvels.system.permission.service.PositionService;
import com.marvels.system.permission.service.SysRoleService;
import com.marvels.system.permission.service.SysUserService;

@RestController
@RequestMapping("/system/pmsn/service")
public class PermissionServiceController extends BaseController {
	@Autowired
	private OrgService orgService;
	
	@Autowired
	private PositionService positionService;
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private McsmUserLoginLogService mcsmUserLoginLogService;
	
	/**
	 * 
	* @Title: getOrgTree 
	* @Description:  查询组织架构树
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月22日 下午4:57:18
	 */
	@RequestMapping("/getOrgTree")
	@ResponseBody
	public MarvelsResultDTO getOrgTree() {
		List<SysOrgDTO> orgList = null;
		try {
			orgList = orgService.getOrgTree();
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getOrgTree]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), orgList);
	}
	
	/**
	 * 查询机构节点详情
	 * @param org
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/getOrgInfo")
	@ResponseBody
	public MarvelsResultDTO getOrgInfo(@RequestBody SysOrgDTO org){
		SysOrgDTO obj = null;
		try {
			obj = orgService.getOrgInfo(org);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getOrgInfo] org="+org.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), obj);
	}
	
	/**
	 * 
	* @Title: addOrg 
	* @Description:  新增组织机构子节点
	* @param sysOrg
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月22日 下午4:57:18
	 */
	@RequestMapping("/addOrg")
	@ResponseBody
	public MarvelsResultDTO addOrg(@RequestBody SysOrgDTO sysOrg) {
		SysOrgDTO data = null;
		try {
			data = orgService.saveOrg(sysOrg);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[addOrg] sysOrg="+sysOrg.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), data);
	}
	
	/**
	 * 新增组织机构根节点
	 * @param sysOrg
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/addRootOrg")
	@ResponseBody
	public MarvelsResultDTO addRootOrg(@RequestBody SysOrgDTO sysOrg) {
		SysOrgDTO data = null;
		try {
			data = orgService.saveRootOrg(sysOrg);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[addRootOrg] sysOrg="+sysOrg.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), data);
	}
	
	/**
	 * 
	* @Title: updateOrg 
	* @Description:  更新组织架构树
	* @param sysOrg
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月22日 下午4:57:18
	 */
	@RequestMapping("/updateOrg")
	@ResponseBody
	public MarvelsResultDTO updateOrg(@RequestBody SysOrgDTO sysOrg) {
		int num = 0;
		try {
			num = orgService.updateOrg(sysOrg);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[updateOrg] sysOrg="+sysOrg.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 
	* @Title: deleteOrg 
	* @Description:  删除组织架构树
	* @param sysOrg
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月22日 下午4:57:18
	 */
	@RequestMapping("/delOrg")
	@ResponseBody
	public MarvelsResultDTO deleteOrg(@RequestBody SysOrgDTO sysOrg) {
		int num = 0;
		try {
			num = orgService.deleteOrg(sysOrg);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[deleteOrg] sysOrg="+sysOrg.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 根据组织代码查询岗位树
	 * @param sysOrg
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/getPositionTreeByOrgCode")
	@ResponseBody
	public MarvelsResultDTO getPositionsByOrgCode(@RequestBody SysOrgDTO sysOrg) {
		List<SysPositionDTO> list = null;
		try {
			list = positionService.getPositionsByOrgCode(sysOrg);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getPositionsByOrgCode] sysOrg="+sysOrg.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
	}
	
	/**
	 * 查询岗位详情
	 * @param position
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/positionDetail")
	@ResponseBody
	public MarvelsResultDTO getPositionById(@RequestBody SysPositionDTO position) {
		SysPositionDTO data = null;
		try {
			data = positionService.getPositionById(position);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getPositionById] position="+position.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), data);
	}
	
	/**
	 * 更新岗位信息
	 * @param position
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/updateByPositionId")
	@ResponseBody
	public MarvelsResultDTO updatePositionById(@RequestBody SysPositionDTO position) {
		int num = 0;
		try {
			num = positionService.updatePositionById(position);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[updatePositionById] position="+position.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 新增岗位
	 * @param position
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/addPosition")
	@ResponseBody
	public MarvelsResultDTO addPosition(@RequestBody SysPositionDTO position) {
		SysPositionDTO data = null;
		try {
			data = positionService.addPosition(position);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[addPosition] position="+position.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), data);
	}
	
	@RequestMapping("/addRootPosition")
	@ResponseBody
	public MarvelsResultDTO addRootPosition(@RequestBody SysPositionDTO position) {
		SysPositionDTO data = null;
		try {
			data = positionService.addRootPosition(position);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[addRootPosition] position="+position.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), data);
	}
	
	/**
	 * 删除岗位
	 * @param position
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/delPosition")
	@ResponseBody
	public MarvelsResultDTO deletePositionById(@RequestBody SysPositionDTO position){
		int num = 0;
		try {
			num = positionService.deletePositionById(position);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[deletePositionById] position="+position.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 
	* @Title: getRolePage 
	* @Description:  获取角色分页列表
	* @param param
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月22日 下午4:57:18
	 */
	@RequestMapping("/rolePageList/{pageNo}/{pageSize}")
	@ResponseBody
	public MarvelsResultDTO getRolePage(@RequestBody SysRoleDTO conditions,@PathVariable Integer pageNo,@PathVariable Integer pageSize){
		Page<SysRoleDTO> pageRole;
		try {
			pageRole = sysRoleService.getRolePage(conditions,pageNo,pageSize);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getRolePage] pageNo="+pageNo+", pageSize="+pageSize+", conditions="+conditions.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), pageRole);
	}
	
	/**
	 * 获取全部角色
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/roleAll")
	@ResponseBody
	public MarvelsResultDTO getAllRoles() {
		List<SysRoleDTO> list = null;
		try {
			list = sysRoleService.getAllRoles();
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getAllRoles]",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
	}
	
	/**
	 * 
	* @Title: addRole
	* @Description:  新增role
	* @param sysrole
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月22日 下午4:57:18
	 */
	@RequestMapping("/addRole")
	@ResponseBody
	public MarvelsResultDTO addRole(@RequestBody SysRoleDTO sysrole) {
		int num = 0;
		try {
			num = sysRoleService.addRole(sysrole);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[addRole] sysrole="+sysrole.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 查询角色详情
	 * @param sysrole
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/roleDetail")
	@ResponseBody
	public MarvelsResultDTO getRoleById(@RequestBody SysRoleDTO sysrole){
		SysRoleDTO data = null;
		try {
			data = sysRoleService.getRoleById(sysrole);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getRoleById] sysrole="+sysrole.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), data);
	}
	
	/**
	 * 
	* @Title: updateRole
	* @Description:  更新role
	* @param sysRole
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月24日 下午4:57:18
	 */
	@RequestMapping("/updateRole")
	@ResponseBody
	public MarvelsResultDTO updateRole(@RequestBody SysRoleDTO sysRole){
		int num = 0;
		try {
			num = sysRoleService.updateRole(sysRole);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[updateRole] sysRole="+sysRole.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 
	* @Title: deleteRole
	* @Description:  删除role
	* @param param
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月22日 下午4:57:18
	 */
	@RequestMapping("/delRole")
	@ResponseBody
	public MarvelsResultDTO deleteRole(@RequestBody SysRoleDTO role) {
		int num = 0;
		try {
			num = sysRoleService.deleteRole(role);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[deleteRole] role="+role.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 添加角色和菜单关系
	 * @param role
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/addMenuRelation")
	@ResponseBody
	public MarvelsResultDTO addMenuRelation(@RequestBody SysRoleDTO role) {
		int num = 0;
		try {
			num = sysRoleService.addMenuRelation(role);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[addMenuRelation] role="+role.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 
	* @Title: getUserPage
	* @Description: 根据组织机构用户分页查询
	* @param param
	* @return MarvelsResultDTO
	* @throws Throwable       
	* @author 李瑶
	* @date 2018年1月24日 下午4:57:18
	 */
	//@Permission("borrowing-export")
	@RequestMapping("/userPageListByOrgCode/{pageNo}/{pageSize}")
	@ResponseBody
	public MarvelsResultDTO getUserPageByOrgCode(@RequestBody SysUserDTO conditions,@PathVariable Integer pageNo,@PathVariable Integer pageSize) {
		Page<SysUserDTO> userPage = null;
		try {
			userPage = sysUserService.getUserPageByOrgCode(conditions,pageNo,pageSize);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getUserPageByOrgCode] pageNo="+pageNo+", pageSize="+pageSize+", conditions="+conditions.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), userPage);
	}
	
	/**
	 * 
	 * @Title: getLoginLog
	 * @Description: 根据组织机构用户分页查询
	 * @param param
	 * @return MarvelsResultDTO
	 * @throws Throwable
	 * @author 涂旭
	 * @date 2018年1月24日 下午4:57:18
	 */
	@RequestMapping("/getLoginLog/{pageNo}/{pageSize}")
	@ResponseBody
	public MarvelsResultDTO getLoginLog(@RequestBody McsmUserLoginLog conditions, @PathVariable Integer pageNo,
			@PathVariable Integer pageSize) {
		Page<McsmUserLoginLog> userPage = null;
		try {
			userPage = mcsmUserLoginLogService.getLoginLog(conditions, pageNo, pageSize);
		} catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("[getLoginLog] pageNo=" + pageNo + ", pageSize=" + pageSize
					+ ", conditions=" + conditions.toString(), e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
				userPage);
	}
	
	/**
	 * 新增用户
	 * @param user
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/addUser")
	@ResponseBody
	public MarvelsResultDTO addUser(@RequestBody SysUserDTO user) {
		int num = 0;
		try {
			num = sysUserService.addUser(user);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[addRole] user="+user.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 查看用户详情
	 * @param user
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/userDetail")
	@ResponseBody
	public MarvelsResultDTO getUserById(@RequestBody SysUserDTO user) {
		SysUserDTO data = null;
		try {
			data = sysUserService.getUserById(user);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[getUserById] user="+user.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), data);
	}
	
	/**
	 * 更新用户
	 * @param user
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/updateUser")
	@ResponseBody
	public MarvelsResultDTO updateUser(@RequestBody SysUserDTO user) {
		int num = 0;
		try {
			num = sysUserService.updateUser(user);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[updateUser] user="+user.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
	
	/**
	 * 删除用户
	 * @param user
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping("/delUser")
	@ResponseBody
	public MarvelsResultDTO deleteUser(@RequestBody SysUserDTO user) {
		int num = 0;
		try {
			num = sysUserService.deleteUser(user);
		}catch (MarvelsBusinessException e) {
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error("[deleteUser] user="+user.toString(),e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), num);
	}
}
